/*
 * Project: BoboDesignPattern
 * 
 * File Created at 2020年5月28日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package test.studyjdk.thread.procom;

import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Type AwaitSignalP
 * @Desc
 * @author 52489
 * @date 2020年5月28日
 * @Version V1.0
 */
public class AwaitSignalP {
    // 仓库最大存储量
    private final int MAX_SIZE = 10;
    // 仓库存储的载体
    private LinkedList<Object> list = new LinkedList<Object>();
    // 锁
    private final Lock lock = new ReentrantLock();
    // 仓库满的条件变量
    private final Condition full = lock.newCondition();
    // 仓库空的条件变量
    // private final Condition empty = lock.newCondition();

    public void produce() {
        // 获得锁
        lock.lock();
        while (list.size() + 1 > MAX_SIZE) {
            System.out.println("【生产者" + Thread.currentThread().getName() + "】仓库已满");
            try {
                full.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        list.add(new Object());
        System.out.println("【生产者" + Thread.currentThread().getName() + "】生产一个产品，现库存" + list.size());

        // empty.signalAll();
        full.signalAll();
        lock.unlock();
    }

    public void consume() {
        // 获得锁
        lock.lock();
        while (list.size() == 0) {
            System.out.println("【消费者" + Thread.currentThread().getName() + "】仓库为空");
            try {
                // empty.await();
                full.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        list.remove();
        System.out.println("【消费者" + Thread.currentThread().getName() + "】消费一个产品，现库存" + list.size());

        full.signalAll();
        lock.unlock();
    }

    public static void main(String[] args) {
        AwaitSignalP awaitSignalP = new AwaitSignalP();
        Thread p1 = new Thread(new P1(awaitSignalP));
        Thread p2 = new Thread(new P1(awaitSignalP));
        Thread p3 = new Thread(new P1(awaitSignalP));

        Thread c1 = new Thread(new C1(awaitSignalP));
        Thread c2 = new Thread(new C1(awaitSignalP));

        p1.start();
        p2.start();
        p3.start();
        c1.start();
        c2.start();
    }
}

class C1 implements Runnable {
    private AwaitSignalP awaitSignalP;

    public C1(AwaitSignalP awaitSignalP) {
        this.awaitSignalP = awaitSignalP;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(2000);
                awaitSignalP.consume();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}

class P1 implements Runnable {

    private AwaitSignalP awaitSignalP;

    public P1(AwaitSignalP awaitSignalP) {
        this.awaitSignalP = awaitSignalP;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
                awaitSignalP.produce();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
